package com.lxgzhw.student_manager02.service.impl;

import com.lxgzhw.student_manager02.dao.StudentDao;
import com.lxgzhw.student_manager02.domain.Result;
import com.lxgzhw.student_manager02.domain.Student;
import com.lxgzhw.student_manager02.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public boolean add(Student student) {
        boolean flag = true;
        try {
            studentDao.save(student);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    @Override
    public Result<Student> page(Integer curr, Integer limit) {
        long count = studentDao.count();
        List<Student> students = studentDao.findLimit((curr - 1) * limit, limit);
        Result<Student> studentResult = new Result<>();
        studentResult.setCode(0);
        studentResult.setMsg("");
        studentResult.setCount(count);
        studentResult.setData(students);
        return studentResult;
    }

    @Override
    public Student get(Integer id) {
        return studentDao.getOne(id);
    }

    @Override
    public void delete(Integer id) {
        studentDao.deleteById(id);
    }
}
